﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class nRhombusVBOData
{
    Vector3 _root = Vector3.zero;

    //
    public Vector3 _left = Vector3.zero;
    public Vector3 _top = Vector3.zero;
    public Vector3 _right = Vector3.zero;
    public Vector3 _bottom = Vector3.zero;
    public Vector3 _center = Vector3.zero;

    public float _h_Diagonal = 1;
    public float _v_Diagonal = 1;

    public float _width = 1;
    public float _height = 1;

    public Vector3 _dir_up = Vector3.zero;
    public Vector3 _dir_dn = Vector3.zero;

    public float _dis = 1;
    public int _sec = 1;

    public Vector3[] _grids = null;
    public int[] _grids_block = null;

    //  菱形的点坐标
    public Vector3[] _vertexts = null;

    /// <summary>
    /// 长短对角线
    /// </summary>
    /// <param name="h"></param>
    /// <param name="v"></param>
    public nRhombusVBOData(float h, float v)
    {
        _h_Diagonal = h;
        _v_Diagonal = v;
        _sec = Mathf.RoundToInt(h / ZoneCfg.RhombusMin);

        CalBase();
    }

    public nRhombusVBOData(float h, float v, Vector3 root)
    {
        _h_Diagonal = h;
        _v_Diagonal = v;
        _sec = Mathf.RoundToInt(h / ZoneCfg.RhombusMin);

        _root = root;

        CalBase();
    }

    void CalBase()
    {
        _left = _root + new Vector3(0, _v_Diagonal / 2, 0);
        _top = _root + new Vector3(_h_Diagonal / 2, _v_Diagonal, 0);
        _right = _root + new Vector3(_h_Diagonal, _v_Diagonal / 2, 0);
        _bottom = _root + new Vector3(_h_Diagonal / 2, 0, 0);
        _vertexts = new Vector3[4] { _left, _top, _right, _bottom };

        _center = _root + new Vector3(_h_Diagonal / 2, _v_Diagonal / 2, 0);

        _dir_up = (_top - _left).normalized;
        _dir_dn = (_bottom - _left).normalized;

        _dis = Vector3.Distance(_left, _top);

        CalRhombusGridBlocks();
    }

    void CalRhombusGridBlocks()
    {
        float n = _h_Diagonal / ZoneCfg.RhombusMin;

        int count = Mathf.RoundToInt(n) * Mathf.RoundToInt(n);

        float sec_dis = _dis / Mathf.RoundToInt(n);

        List<Vector3> list = new List<Vector3>(count);
        List<int> listblock = new List<int>(count);

        int col = 0;
        int row = 0;

        for (row = 0; row < n; ++row)
        {
            for (col = 0; col < n; ++col)
            {
                Vector3 x = _dir_dn * row * sec_dis;
                Vector3 y = _dir_up * col * sec_dis;
                Vector3 pos = x + y + _left;

                list.Add(pos);
                listblock.Add(0);
            }
        }

        _grids = list.ToArray();
        _grids_block = listblock.ToArray();

    }
}
